package com.cebbank.sz.cebtemplate.utils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author sucz
 * @create 2019/12/5 18:14
 */
public class DateUtil {

    public static final String YMD_SHORT = "yyyyMMdd";
    public static final String YMD_DASH = "yyyy-MM-dd";
    public static final String YMD_SLASH = "yyyy/MM/dd";
    public static final String YMDHMS_SHORT = "yyyyMMddHHmmss";
    public static final String YMDHMS_DASH = "yyyy-MM-dd HH:mm:ss";
    public static final String YMDHMS_SLASH = "yyyy/MM/dd HH:mm:ss";

    public static final DateTimeFormatter DATE_FORMATTER_SHORT = DateTimeFormatter.ofPattern(YMD_SHORT);
    public static final DateTimeFormatter DATE_FORMATTER_DASH = DateTimeFormatter.ofPattern(YMD_DASH);
    public static final DateTimeFormatter DATE_FORMATTER_SLASH = DateTimeFormatter.ofPattern(YMD_SLASH);
    public static final DateTimeFormatter DATETIME_FORMATTER_SHORT = DateTimeFormatter.ofPattern(YMDHMS_SHORT);
    public static final DateTimeFormatter DATETIME_FORMATTER_DASH = DateTimeFormatter.ofPattern(YMDHMS_DASH);
    public static final DateTimeFormatter DATETIME_FORMATTER_SLASH = DateTimeFormatter.ofPattern(YMDHMS_SLASH);

    /**
     * 获取当前日期
     * @return
     */
    public static LocalDate getCurrentLocalDate(){
        return LocalDate.now();
    }

    /**
     * 获取当前日期时间
     * @return
     */
    public static LocalDateTime getCurrentLocalDateTime(){
        return LocalDateTime.now();
    }

    /**
     * 获取当前日期：yyyyMMdd
     * @return
     */
    public static String getCurrentLocalDateShort(){
        return LocalDate.now().format(DATE_FORMATTER_SHORT);
    }

    /**
     * 获取当前日期：yyyy-MM-dd
     * @return
     */
    public static String getCurrentLocalDateDash(){
        return LocalDate.now().format(DATE_FORMATTER_DASH);
    }

    /**
     * 获取当前日期：yyyy/MM/dd
     * @return
     */
    public static String getCurrentLocalDateSlash(){
        return LocalDate.now().format(DATE_FORMATTER_SLASH);
    }

    /**
     * 获取当前日期时间：yyyyMMddHHmmss
     * @return
     */
    public static String getCurrentLocalDateTimeShort(){
        return LocalDateTime.now().format(DATETIME_FORMATTER_SHORT);
    }

    /**
     * 获取当前日期时间：yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String getCurrentLocalDateTimeDash(){
        return LocalDateTime.now().format(DATETIME_FORMATTER_DASH);
    }

    /**
     * 获取当前日期时间：yyyy/MM/dd HH:mm:ss
     * @return
     */
    public static String getCurrentLocalDateTimeSlash(){
        return LocalDateTime.now().format(DATETIME_FORMATTER_SLASH);
    }

    /**
     * 日期格式转换
     * @param sourceDate 原日期字符串
     * @param sourcePatter 原日期格式
     * @param targetPatter 目标日期格式
     * @return
     */
    public static String translateDateFormat(String sourceDate, String sourcePatter, String targetPatter){
        LocalDate localDate = LocalDate.parse(sourceDate, DateTimeFormatter.ofPattern(sourcePatter));
        return localDate.format(DateTimeFormatter.ofPattern(targetPatter));
    }

    /**
     * 日期时间格式转换
     * @param sourceDateTime 原日期时间字符串
     * @param sourcePatter 原日期时间格式
     * @param targetPatter 目标日期时间格式
     * @return
     */
    public static String translateDateTimeFormat(String sourceDateTime, String sourcePatter, String targetPatter){
        LocalDateTime localDateTime = LocalDateTime.parse(sourceDateTime, DateTimeFormatter.ofPattern(sourcePatter));
        return localDateTime.format(DateTimeFormatter.ofPattern(targetPatter));
    }

    /**
     * 日期格式化
     * @param patter 格式
     * @return
     */
    public static String formatDate(String patter){
        return LocalDate.now().format(DateTimeFormatter.ofPattern(patter));
    }

    /**
     * 日期格式化
     * @param patter 格式
     * @param date 时间
     * @return
     */
    public static String formatDate(String patter, LocalDate date){
        return date.format(DateTimeFormatter.ofPattern(patter));
    }

    /**
     * 日期时间格式化
     * @param patter 格式
     * @return
     */
    public static String formatDateTime(String patter){
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(patter));
    }

    /**
     * 日期时间格式化
     * @param patter 格式
     * @param dateTime 日期时间
     * @return
     */
    public static String formatDateTime(String patter, LocalDateTime dateTime){
        return dateTime.format(DateTimeFormatter.ofPattern(patter));
    }

    /**
     * 在原有日期上加减月数
     * @param patter 日期格式
     * @param sourceDate 基础日期
     * @param month 加减的月数
     * @return
     */
    public static String stepMonth(String patter, String sourceDate, long month) {
        LocalDate localDate = LocalDate.parse(sourceDate, DateTimeFormatter.ofPattern(patter));
        return localDate.plusMonths(month).format(DateTimeFormatter.ofPattern(patter));
    }

    /**
     * 在原有日期上加减年
     * @param patter 日期格式
     * @param sourceDate 基础日期
     * @param year 加减的年数
     * @return
     */
    public static String stepYear(String patter, String sourceDate, long year) {
        LocalDate localDate = LocalDate.parse(sourceDate, DateTimeFormatter.ofPattern(patter));
        return localDate.plusYears(year).format(DateTimeFormatter.ofPattern(patter));
    }

}
